package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.UserRoleDAO;
import com.example.cursor_orm.model.UserRole;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserRoleDAOImpl implements UserRoleDAO {
    
    @Override
    public int insert(UserRole userRole) {
        String sql = "INSERT INTO user_role (user_id, role_id) VALUES (?, ?)";
        return DatabaseUtil.executeUpdate(sql, userRole.getUserId(), userRole.getRoleId());
    }
    
    @Override
    public int deleteById(Integer id) {
        // UserRole表没有id字段，使用复合主键删除
        String sql = "DELETE FROM user_role WHERE user_id = ? AND role_id = ?";
        return DatabaseUtil.executeUpdate(sql, id, id);
    }
    
    @Override
    public int update(UserRole userRole) {
        // UserRole表没有id字段，无法更新，只能删除后重新插入
        return 0;
    }
    
    @Override
    public UserRole findById(Integer id) {
        // UserRole表没有id字段，无法通过id查询
        return null;
    }
    
    @Override
    public List<UserRole> findAll() {
        String sql = "SELECT * FROM user_role ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToUserRoleList(rs);
    }
    
    @Override
    public List<UserRole> findByCondition(String condition) {
        String sql = "SELECT * FROM user_role WHERE " + condition + " ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToUserRoleList(rs);
    }
    
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM user_role";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
    public List<UserRole> findByUserId(Integer userId) {
        String sql = "SELECT * FROM user_role WHERE user_id = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId);
        return mapResultSetToUserRoleList(rs);
    }
    
    @Override
    public List<UserRole> findByRoleId(Integer roleId) {
        String sql = "SELECT * FROM user_role WHERE role_id = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, roleId);
        return mapResultSetToUserRoleList(rs);
    }
    
    @Override
    public int assignRoleToUser(Integer userId, Integer roleId) {
        String sql = "INSERT INTO user_role (user_id, role_id) VALUES (?, ?)";
        return DatabaseUtil.executeUpdate(sql, userId, roleId);
    }
    
    @Override
    public int removeRoleFromUser(Integer userId, Integer roleId) {
        String sql = "DELETE FROM user_role WHERE user_id = ? AND role_id = ?";
        return DatabaseUtil.executeUpdate(sql, userId, roleId);
    }
    
    @Override
    public int deleteByUserId(Integer userId) {
        String sql = "DELETE FROM user_role WHERE user_id = ?";
        return DatabaseUtil.executeUpdate(sql, userId);
    }
    
    @Override
    public int deleteByRoleId(Integer roleId) {
        String sql = "DELETE FROM user_role WHERE role_id = ?";
        return DatabaseUtil.executeUpdate(sql, roleId);
    }
    
    @Override
    public boolean hasRole(Integer userId, Integer roleId) {
        String sql = "SELECT COUNT(*) FROM user_role WHERE user_id = ? AND role_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId, roleId);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 将ResultSet映射为UserRole对象
     */
    private UserRole mapResultSetToUserRole(ResultSet rs) {
        try {
            if (rs != null && rs.next()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(rs.getInt("user_id"));
                userRole.setRoleId(rs.getInt("role_id"));
                return userRole;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将ResultSet映射为UserRole对象列表
     */
    private List<UserRole> mapResultSetToUserRoleList(ResultSet rs) {
        List<UserRole> userRoles = new ArrayList<>();
        try {
            if (rs != null) {
                while (rs.next()) {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(rs.getInt("user_id"));
                    userRole.setRoleId(rs.getInt("role_id"));
                    userRoles.add(userRole);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userRoles;
    }
} 